﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLL;
using DAL;

namespace CuaHangBangDia
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void loadfrom()
        {
            HangHoacontrol hanghoa = new HangHoacontrol();
            hanghoa.getall();
            dtgview.DataSource = hanghoa.Data;
            hanghoa.GetallLoai();
            dtgridloaihang.DataSource = hanghoa.Data;
            hanghoa.GetallNPP();
            dtgridnpp.DataSource = hanghoa.Data;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            loadfrom();
        }

        private void btntimHH_Click(object sender, EventArgs e)
        {
            HangHoacontrol hanghoa = new HangHoacontrol();
            hanghoa.getall();
            DataView view = new DataView(hanghoa.Data);
            string s = "";
            TimKiemHH frm = new TimKiemHH();
            frm.ShowDialog();
            if (frm.kieutim != -1 && frm.thongtintim != "")
            {
                switch (frm.kieutim)
                {
                    case 0:
                        s = "MaHangHoa=" + frm.thongtintim + "";
                        break;
                    case 1:
                        s = "TenHangHoa like '%" + frm.thongtintim + "%'";
                        break;
                    case 2:
                        s = "LoaiHangHoa=" + frm.thongtintim + "";
                        break;
                }
            }

            view.RowFilter = s;
            dtgview.DataSource = view;
        }

        private void btntimlh_Click(object sender, EventArgs e)
        {
            HangHoacontrol hanghoa = new HangHoacontrol();
            hanghoa.GetallLoai();
            DataView view = new DataView(hanghoa.Data);
            string s = "";
            TimKiemLoai frm = new TimKiemLoai();
            frm.ShowDialog();
            if (frm.kieutim != -1 && frm.thongtintim != "")
            {
                switch (frm.kieutim)
                {
                    case 0:
                        s = "MaLoai=" + frm.thongtintim + "";
                        break;
                    case 1:
                        s = "TenLoai like '%" + frm.thongtintim + "%'";
                        break;
                    case 2:
                        s = "NhaPhanPhoi=" + frm.thongtintim + "";
                        break;
                }
            }

            view.RowFilter = s;
            dtgridloaihang.DataSource = view;
        }

        private void dtgview_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dtgview.SelectedRows.Count > 0)
            {
                DataGridViewRow selectedrow = dtgview.SelectedRows[0];
                DataRowView rowview = selectedrow.DataBoundItem as DataRowView;
                ShowFormhh(rowview);
            }
        }
        private void ShowFormhh(DataRowView rowview)
        {
            this.txbmaHH.Text = rowview[0].ToString();
            this.txbtenhh.Text = rowview[1].ToString();
            this.txbloaihh.Text = rowview[2].ToString();
            this.txbsoluonghh.Text = rowview[3].ToString();
            this.txbgiabanhh.Text = rowview[4].ToString();
            this.txbgiamuahh.Text = rowview[5].ToString();



        }
        private void ShowFormLH(DataRowView rowviewLH)
        {
            this.txbmaloai.Text = rowviewLH[0].ToString();
            this.txbtenLH.Text = rowviewLH[1].ToString();
            this.txbLoaiNPP.Text = rowviewLH[2].ToString();
            this.txbmota.Text = rowviewLH[3].ToString();
        }
        private void dtgridloaihang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dtgridloaihang.SelectedRows.Count > 0)
            {
                DataGridViewRow selectedrow = dtgridloaihang.SelectedRows[0];
                DataRowView rowviewLH = selectedrow.DataBoundItem as DataRowView;
                ShowFormLH(rowviewLH);
            }
        }
        private void ShowFormNPP(DataRowView rowviewNPP)
        {
            this.txbmaNPP.Text = rowviewNPP[0].ToString();
            this.txbtenNPP.Text = rowviewNPP[1].ToString();
            this.txbDiaChi.Text = rowviewNPP[2].ToString();
            this.txbemail.Text = rowviewNPP[3].ToString();
            this.txbSDT.Text = rowviewNPP[4].ToString();
        }

        private void dtgridnpp_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dtgridnpp.SelectedRows.Count > 0)
            {
                DataGridViewRow selectedrow = dtgridnpp.SelectedRows[0];
                DataRowView rowviewNPP = selectedrow.DataBoundItem as DataRowView;
                ShowFormNPP(rowviewNPP);
            }
        }

        private void btntimnpp_Click(object sender, EventArgs e)
        {
            HangHoacontrol hanghoa = new HangHoacontrol();
            hanghoa.GetallNPP();
            DataView view = new DataView(hanghoa.Data);
            string s = "";
            TimkiemNPP frm = new TimkiemNPP();
            frm.ShowDialog();
            if (frm.kieutim != -1 && frm.thongtintim != "")
            {
                switch (frm.kieutim)
                {
                    case 0:
                        s = "MaNhaPhanPhoi=" + frm.thongtintim + "";
                        break;
                    case 1:
                        s = "TenNhaPhanPhoi like '%" + frm.thongtintim + "%'";
                        break;
                    case 3:
                        s = "Email like '%" + frm.thongtintim + "%'";
                        break;
                    case 2:
                        s = "SoDienThoai like'%" + frm.thongtintim + "%'";
                        break;
                    case 5:
                        s = "DiaChi like '%" + frm.thongtintim + "%'";
                        break;
                }
                view.RowFilter = s;
                dtgridnpp.DataSource = view;
            }



        }

        private void btnthemH_Click(object sender, EventArgs e)
        {
            FormThemCapnhatHangHoa frm = new FormThemCapnhatHangHoa(0);
            frm.ShowDialog();
            loadfrom();
        }

        private void btnthemNPP_Click(object sender, EventArgs e)
        {
            Formthemcapnhatnpp frm = new Formthemcapnhatnpp(0);
            frm.ShowDialog();
            loadfrom();
        }

       

        private void btnsuaNPP_Click(object sender, EventArgs e)
        {
            NHA_PHAN_PHOI npp = new NHA_PHAN_PHOI();
            if (dtgridnpp.SelectedRows[0] != null)
            {
                Formthemcapnhatnpp frm = new Formthemcapnhatnpp(1);
                frm.Nhapp = LayNPPGRV();
                frm.ShowDialog();
                loadfrom();
            }
            else
                MessageBox.Show("Chọn nhà phân phối cần cập nhật", "Thông báo");
        }

        private void btnsuaH_Click(object sender, EventArgs e)
        {
            HANG_HOA hang = new HANG_HOA();
            if (dtgview.SelectedRows[0] != null)
            {
                FormThemCapnhatHangHoa frm = new FormThemCapnhatHangHoa(1);
                frm.Hanghoa = LayHangHoaGRV();
                frm.ShowDialog();
                loadfrom();
            }
            else
                MessageBox.Show("Chọn hàng hóa cần cập nhật","Thông báo");
        }

        private void btnxoaH_Click(object sender, EventArgs e)
        {
            HangHoacontrol controler = new HangHoacontrol();
            HANG_HOA hang = new HANG_HOA();
            if (dtgview.SelectedRows[0] != null)
            {
                hang = LayHangHoaGRV();
                if (controler.XoaHH(hang) > 0)
                {
                    MessageBox.Show("Xóa hàng hóa thành công", "Thông báo");
                }
                else
                    MessageBox.Show("Lỗi khi đang xóa hàng hóa");
                dtgview.Rows.Remove(dtgview.SelectedRows[0]);
            }
            else
                MessageBox.Show("Chọn hàng hóa muốn xóa", "Thông báo");
        }

        private HANG_HOA LayHangHoaGRV()
        {
            DataGridViewRow selectedrow = dtgview.SelectedRows[0];
            DataRowView rowview = selectedrow.DataBoundItem as DataRowView;
            HANG_HOA hang = new HANG_HOA();
            hang.MaHangHoa = (int)rowview["MaHangHoa"]; hang.TenHangHoa = rowview["TenHangHoa"].ToString();
            hang.LoaiHangHoa = (int)rowview["LoaihangHoa"]; hang.SoLuong = (int)rowview["SoLuong"];
            hang.GiaBan = (decimal)rowview["GiaBan"]; hang.GiaMua = (decimal)rowview["GiaMua"];
            return hang;
        }
        private void btnxoaLH_Click(object sender, EventArgs e)
        {
            HangHoacontrol controler = new HangHoacontrol();
            LOAI_HANG_HOA loaihang = new LOAI_HANG_HOA();
            if (dtgridloaihang.SelectedRows[0] != null)
            {
                loaihang = LayLoaiHangHoaGRV();
            if (controler.XoaLH(loaihang) > 0)
                {
                    MessageBox.Show("Xóa loại hàng hóa thành công", "Thông báo");
                }
                else
                    MessageBox.Show("Lỗi khi đang xóa loại hàng hóa");
                dtgridloaihang.Rows.Remove(dtgridloaihang.SelectedRows[0]);
            }
            else
                MessageBox.Show("Chọn loại hàng hóa muốn xóa", "Thông báo");
        }

        private void btnthemLH_Click(object sender, EventArgs e)
        {
            FormThemCapnhatLoaiHang frm = new FormThemCapnhatLoaiHang(0);
            frm.ShowDialog();
            loadfrom();

        }

        private LOAI_HANG_HOA LayLoaiHangHoaGRV()
        {
            DataGridViewRow selectedrow = dtgridloaihang.SelectedRows[0];
            DataRowView rowview = selectedrow.DataBoundItem as DataRowView;
            LOAI_HANG_HOA hang = new LOAI_HANG_HOA();
            hang.MaLoai = (int)rowview["MaLoai"]; hang.TenLoai = rowview["TenLoai"].ToString();
            hang.NhaPhanPhoi = (int)rowview["NhaPhanPhoi"]; hang.MoTa = rowview["MoTa"].ToString();
            return hang;
        }

        private void btnsuaLH_Click(object sender, EventArgs e)
        {
            LOAI_HANG_HOA hang = new LOAI_HANG_HOA();
            if (dtgridloaihang.SelectedRows[0] != null)
            {
                FormThemCapnhatLoaiHang frm = new FormThemCapnhatLoaiHang(1);
                frm.Loai_HH = LayLoaiHangHoaGRV();
                frm.ShowDialog();
                loadfrom();
            }
            else
                MessageBox.Show("Chọn loại hàng hóa cần cập nhật", "Thông báo");
        }

        private void btnxoaNPP_Click(object sender, EventArgs e)
        {
            HangHoacontrol controler = new HangHoacontrol();
            NHA_PHAN_PHOI npp = new NHA_PHAN_PHOI();
            if (dtgridnpp.SelectedRows[0] != null)
            {
                npp = LayNPPGRV();
                if (controler.XoaNPP(npp) > 0)
                {
                    MessageBox.Show("Xóa nhà phân phối thành công", "Thông báo");
                }
                else
                    MessageBox.Show("Lỗi khi đang xóa loại hàng hóa");
                dtgridnpp.Rows.Remove(dtgridnpp.SelectedRows[0]);
            }
            else
                MessageBox.Show("Chọn nhà phân phối muốn xóa", "Thông báo");
        }
        private NHA_PHAN_PHOI LayNPPGRV()
        {
            DataGridViewRow selectedrow = dtgridnpp.SelectedRows[0];
            DataRowView rowview = selectedrow.DataBoundItem as DataRowView;
            NHA_PHAN_PHOI npp = new NHA_PHAN_PHOI();
            npp.MaNhaPhanPhoi = (int)rowview["MaNhaPhanPhoi"]; npp.TenNhaPhanPhoi = rowview["TenNhaPhanPhoi"].ToString();
            npp.DiaChi = rowview["DiaChi"].ToString(); npp.Email = rowview["Email"].ToString();
            npp.SoDienThoai = rowview["SoDienThoai"].ToString();
            return npp;
        }

     
    }
}